package cn.kai.ums.controller;

import cn.kai.ums.mapper.UmsTeacherMapper;
import cn.kai.ums.model.UmsStudent;
import cn.kai.ums.model.UmsTeacher;
import cn.kai.ums.service.UmsStudentService;
import cn.kai.ums.service.UmsTeacherService;
import cn.kai.ums.tools.JsonData;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("ums/v1/teacher")
@Api(tags = "教师控制类")
public class TeacherController {

    @Autowired
    private UmsTeacherService umsTeacherService;

    @Autowired
    private UmsTeacherMapper umsTeacherMapper;

    @Autowired
    protected UmsStudentService umsStudentService;


    @ApiOperation("根据老师编号和姓名进行模糊查询")
    @GetMapping
    public JsonData getTeacherlist(@RequestParam(defaultValue = "1",required = false) Integer pageNum
            ,@RequestParam(defaultValue = "5",required = false) Integer pageSize
            ,@RequestParam(defaultValue = "",value = "likeTeacher",required = false) String likeTeacher){
        Page<UmsTeacher> umsTeacherPage = umsTeacherMapper.selectPage(new Page<>(pageNum,pageSize)
                , Wrappers.<UmsTeacher>lambdaQuery()
                        .like(UmsTeacher::getTeacherNo,likeTeacher)
                        .or()
                        .like(UmsTeacher::getTeacherName,likeTeacher));
        return JsonData.buildSuccess(umsTeacherPage);
    }


    @PostMapping
    public JsonData saveTeacher(@RequestBody UmsTeacher umsTeacher){
        UmsTeacher umsTeacher1 = umsTeacherService.getOneTea(umsTeacher.getTeacherNo());
        if(umsTeacher1==null){
            umsTeacher.setTeacherCreatetime(new Date());
            return JsonData.buildSuccess(umsTeacherService.save(umsTeacher));
        }else {
            return JsonData.buildError("教师编号不可重复");
        }
    }

    @PutMapping
    public JsonData updateTeacher(@RequestBody UmsTeacher umsTeacher){
        return JsonData.buildSuccess(umsTeacherService.updateById(umsTeacher));
    }

    @DeleteMapping
    public JsonData delTeacher(@RequestParam Integer teaid){
        return JsonData.buildSuccess(umsTeacherService.removeById(teaid));
    }

    @PostMapping("/delBatchTeacher")
    public JsonData delBatchTeacher(@RequestBody List<Integer> teaids){
        return JsonData.buildSuccess(umsTeacherService.removeByIds(teaids));
    }

    @ApiOperation("老师个人课程下的全部学生信息")
    @GetMapping("/teaCourseStudent")
    public JsonData teaCourseStudent(@RequestParam String teaid){
        List<UmsStudent> umsStudents = umsStudentService.teaCourseStudent(teaid);
        return JsonData.buildSuccess(umsStudents);
    }


}
